import axios from 'axios'
import { ElNotification } from 'element-plus'

const service = axios.create({
    timeout: 3000
})

service.interceptors.request.use(config => {
    if (localStorage.getItem('token')) {
        config.transformRequest = [data => {
            data = data || {}
            data.token = localStorage.getItem('token')
            return JSON.stringify(data) 
        }]
    }
    return config
})

service.interceptors.response.use(res => {
    if (res.data.code == 1) {
        ElNotification.error(res.data.msg)
        Promise.reject(res.data.msg)
    }
    return res.data
})

export default service
